{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import cnmaps\n",
    "import numpy as np\n",
    "from shapely.geometry import Point\n",
    "import glob"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "provincelist=['广西壮族自治区','广东省','海南省','福建省','台湾省','浙江省','江苏省','上海市','山东省','辽宁省']\n",
    "filepath=glob.glob(r'D:\\WORKcode\\pythoncode\\ncdata\\typath\\CMA_csv\\*')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2003\n",
      "2004\n",
      "2005\n",
      "2006\n",
      "2007\n",
      "2008\n",
      "2009\n",
      "2010\n",
      "2011\n",
      "2012\n",
      "2013\n",
      "2014\n",
      "2015\n",
      "2016\n",
      "2017\n",
      "2018\n",
      "2019\n"
     ]
    }
   ],
   "source": [
    "# 字典计数器 记录各省台风的登陆个数\n",
    "countdict={}\n",
    "for i in filepath:\n",
    "    # print(i[-8:-4])\n",
    "    if int(i[-8:-4]) not in np.arange(2003,2020,1):\n",
    "        continue\n",
    "    print(i[-8:-4])\n",
    "    countdict[str(i[-8:-4])]={}\n",
    "    # 计数器初始化\n",
    "    for province_i in provincelist:\n",
    "        countdict[str(i[-8:-4])][province_i]=0\n",
    "    df=pd.read_excel(i)\n",
    "    # 去除重复的名字\n",
    "    namelist=df['name_en'].drop_duplicates().reset_index()['name_en']\n",
    "    for name_i in namelist:\n",
    "        temp=df[df['name_en']==name_i]\n",
    "        out_proviceloop=False\n",
    "        '''判断这个台风在那个省份登陆，循环判断'''\n",
    "        for province_i in provincelist:\n",
    "            # 把这个省份(区)的区域调出来\n",
    "            Pmap=cnmaps.get_adm_maps(province=province_i,engine='geopandas',only_polygon=False)['geometry'][0]\n",
    "            for lat_i,lon_i in zip(temp['lat'],temp['lon']):\n",
    "                '''判断这个台风的这个点是否在省(区)的区域内,如果是计数器加一,继续下一循环,以此类推,直到所有'''\n",
    "                if Pmap.contains(Point(lon_i,lat_i)):\n",
    "                    countdict[str(i[-8:-4])][province_i]+=1\n",
    "                    # out_proviceloop=True\n",
    "                    break\n",
    "            # if out_proviceloop:\n",
    "            #     '''如果台风已经判断在哪里登陆了，直接跳出两层循环，开始下一个'''\n",
    "            #     break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'2003': {'广西壮族自治区': 3,\n",
       "  '广东省': 2,\n",
       "  '海南省': 2,\n",
       "  '福建省': 1,\n",
       "  '台湾省': 1,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 0,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2004': {'广西壮族自治区': 0,\n",
       "  '广东省': 2,\n",
       "  '海南省': 0,\n",
       "  '福建省': 1,\n",
       "  '台湾省': 3,\n",
       "  '浙江省': 3,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 0,\n",
       "  '山东省': 1,\n",
       "  '辽宁省': 0},\n",
       " '2005': {'广西壮族自治区': 0,\n",
       "  '广东省': 1,\n",
       "  '海南省': 2,\n",
       "  '福建省': 3,\n",
       "  '台湾省': 2,\n",
       "  '浙江省': 2,\n",
       "  '江苏省': 2,\n",
       "  '上海市': 0,\n",
       "  '山东省': 1,\n",
       "  '辽宁省': 1},\n",
       " '2006': {'广西壮族自治区': 3,\n",
       "  '广东省': 2,\n",
       "  '海南省': 0,\n",
       "  '福建省': 4,\n",
       "  '台湾省': 2,\n",
       "  '浙江省': 0,\n",
       "  '江苏省': 0,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2007': {'广西壮族自治区': 0,\n",
       "  '广东省': 1,\n",
       "  '海南省': 2,\n",
       "  '福建省': 2,\n",
       "  '台湾省': 1,\n",
       "  '浙江省': 2,\n",
       "  '江苏省': 0,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2008': {'广西壮族自治区': 1,\n",
       "  '广东省': 6,\n",
       "  '海南省': 1,\n",
       "  '福建省': 2,\n",
       "  '台湾省': 4,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2009': {'广西壮族自治区': 2,\n",
       "  '广东省': 5,\n",
       "  '海南省': 2,\n",
       "  '福建省': 2,\n",
       "  '台湾省': 1,\n",
       "  '浙江省': 2,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2010': {'广西壮族自治区': 2,\n",
       "  '广东省': 3,\n",
       "  '海南省': 1,\n",
       "  '福建省': 3,\n",
       "  '台湾省': 1,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 0,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2011': {'广西壮族自治区': 0,\n",
       "  '广东省': 2,\n",
       "  '海南省': 3,\n",
       "  '福建省': 1,\n",
       "  '台湾省': 1,\n",
       "  '浙江省': 0,\n",
       "  '江苏省': 0,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2012': {'广西壮族自治区': 2,\n",
       "  '广东省': 3,\n",
       "  '海南省': 1,\n",
       "  '福建省': 1,\n",
       "  '台湾省': 0,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 0,\n",
       "  '山东省': 1,\n",
       "  '辽宁省': 0},\n",
       " '2013': {'广西壮族自治区': 4,\n",
       "  '广东省': 3,\n",
       "  '海南省': 3,\n",
       "  '福建省': 4,\n",
       "  '台湾省': 0,\n",
       "  '浙江省': 0,\n",
       "  '江苏省': 0,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2014': {'广西壮族自治区': 1,\n",
       "  '广东省': 2,\n",
       "  '海南省': 0,\n",
       "  '福建省': 2,\n",
       "  '台湾省': 1,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 1,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2015': {'广西壮族自治区': 1,\n",
       "  '广东省': 1,\n",
       "  '海南省': 1,\n",
       "  '福建省': 2,\n",
       "  '台湾省': 2,\n",
       "  '浙江省': 0,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2016': {'广西壮族自治区': 2,\n",
       "  '广东省': 2,\n",
       "  '海南省': 2,\n",
       "  '福建省': 3,\n",
       "  '台湾省': 2,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 0,\n",
       "  '山东省': 0,\n",
       "  '辽宁省': 0},\n",
       " '2017': {'广西壮族自治区': 2,\n",
       "  '广东省': 5,\n",
       "  '海南省': 1,\n",
       "  '福建省': 3,\n",
       "  '台湾省': 2,\n",
       "  '浙江省': 0,\n",
       "  '江苏省': 0,\n",
       "  '上海市': 0,\n",
       "  '山东省': 1,\n",
       "  '辽宁省': 0},\n",
       " '2018': {'广西壮族自治区': 3,\n",
       "  '广东省': 5,\n",
       "  '海南省': 4,\n",
       "  '福建省': 1,\n",
       "  '台湾省': 1,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 3,\n",
       "  '上海市': 2,\n",
       "  '山东省': 3,\n",
       "  '辽宁省': 1},\n",
       " '2019': {'广西壮族自治区': 2,\n",
       "  '广东省': 2,\n",
       "  '海南省': 3,\n",
       "  '福建省': 1,\n",
       "  '台湾省': 0,\n",
       "  '浙江省': 1,\n",
       "  '江苏省': 1,\n",
       "  '上海市': 0,\n",
       "  '山东省': 1,\n",
       "  '辽宁省': 0}}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "countdict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>2003</th>\n",
       "      <th>2004</th>\n",
       "      <th>2005</th>\n",
       "      <th>2006</th>\n",
       "      <th>2007</th>\n",
       "      <th>2008</th>\n",
       "      <th>2009</th>\n",
       "      <th>2010</th>\n",
       "      <th>2011</th>\n",
       "      <th>2012</th>\n",
       "      <th>2013</th>\n",
       "      <th>2014</th>\n",
       "      <th>2015</th>\n",
       "      <th>2016</th>\n",
       "      <th>2017</th>\n",
       "      <th>2018</th>\n",
       "      <th>2019</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>广西壮族自治区</th>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>广东省</th>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>6</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>5</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>海南省</th>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>福建省</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>台湾省</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>浙江省</th>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>江苏省</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>上海市</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>山东省</th>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>辽宁省</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  \\\n",
       "广西壮族自治区     3     0     0     3     0     1     2     2     0     2     4   \n",
       "广东省         2     2     1     2     1     6     5     3     2     3     3   \n",
       "海南省         2     0     2     0     2     1     2     1     3     1     3   \n",
       "福建省         1     1     3     4     2     2     2     3     1     1     4   \n",
       "台湾省         1     3     2     2     1     4     1     1     1     0     0   \n",
       "浙江省         1     3     2     0     2     1     2     1     0     1     0   \n",
       "江苏省         0     1     2     0     0     1     1     0     0     1     0   \n",
       "上海市         0     0     0     0     0     0     0     0     0     0     0   \n",
       "山东省         0     1     1     0     0     0     0     0     0     1     0   \n",
       "辽宁省         0     0     1     0     0     0     0     0     0     0     0   \n",
       "\n",
       "         2014  2015  2016  2017  2018  2019  \n",
       "广西壮族自治区     1     1     2     2     3     2  \n",
       "广东省         2     1     2     5     5     2  \n",
       "海南省         0     1     2     1     4     3  \n",
       "福建省         2     2     3     3     1     1  \n",
       "台湾省         1     2     2     2     1     0  \n",
       "浙江省         1     0     1     0     1     1  \n",
       "江苏省         1     1     1     0     3     1  \n",
       "上海市         1     0     0     0     2     0  \n",
       "山东省         0     0     0     1     3     1  \n",
       "辽宁省         0     0     0     0     1     0  "
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 存储为csv\n",
    "df1=pd.DataFrame.from_dict(countdict)\n",
    "df1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.10.4 ('py310')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.4"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "1af7d1ae3c55d82bbda2a5569c7a9f2f2d33ff4c43dd1c666120ac4867346c8c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
